//var idField;
var params;
$.btAddressBook = function(options) {
	this.settings = $.extend(true, {}, $.btAddressBook.defaults, options );
	$.sdAddressBook.settings = this.settings;
	//idField=this.settings.idFieldId;
	params={
			title:this.settings.title,
			openType:this.settings.openType,
			type:this.settings.type,
			userPermission:this.settings.userPermission,
			defaultChecked:this.settings.defaultChecked,
			selectType:this.settings.selectType,
			num:this.settings.num, 
			idFieldId:this.settings.idFieldId,
			textFieldId:this.settings.textFieldId,
			dataFieldId:this.settings.dataFieldId,
			filterId:this.settings.filterId,
			includeParent:this.settings.includeParent,
			defaultParams: this.settings.defaultParams,
			treeLevels : this.settings.treeLevels,
			nodeCode : this.settings.nodeCode,
			registerDicCode : this.settings.registerDicCode
	};
	if(this.settings.openType == 'windows'){
		this.initWindows();
	}else{
	 	this.init();	
	}
};

$.sdAddressBook.settings = function(settings){
	return settings;
}

$.btAddressBook.compIds = function(data){
	var ids = "";
	if (data.length == 1) {
		ids += data[0].id;
		// 下面代码会在id后拼上 !@!
		//ids += data[0].id+$.sdAddressBook.consts.separator+data[0].text;
	} else {
		for (var i = 0; i < data.length; i++) {
			if (i == data.length - 1) {
				ids += data[i].id;
				//ids += data[i].id+$.sdAddressBook.consts.separator+data[i].text;
			} else {
				ids += data[i].id + "&";
				//ids += data[i].id+$.sdAddressBook.consts.separator+data[i].text + "&";
			}
		}
	}
	return ids;
}
$.btAddressBook.compValues = function(data){
	var str = "";
	if (data.length == 1) {
		// 下面代码会在id后拼上 !@!
		//str += data[0].code+$.sdAddressBook.consts.separator+data[0].text+$.sdAddressBook.consts.separator+data[0].id;
		str += data[0].value;
	} else {
		for (var i = 0; i < data.length; i++) {
			if (i == data.length - 1) {
				//str += data[i].code+$.sdAddressBook.consts.separator+data[i].text+$.sdAddressBook.consts.separator+data[i].id;
				str += data[i].value;
			} else {
				//str += data[i].code+$.sdAddressBook.consts.separator+data[i].text+$.sdAddressBook.consts.separator+data[i].id+$.sdAddressBook.consts.objSeparator;
				str += data[i].value+$.sdAddressBook.consts.objSeparator;
			}
		}
	}
	return str;
}
$.btAddressBook.compTexts = function(data){
	var texts = "";
	if (data.length == 1) {
		texts += data[0].text;
	} else {
		for (var i = 0; i < data.length; i++) {
			if (i == data.length - 1) {
				texts += data[i].text;
			} else {
				texts += data[i].text + "、";
			}
		}
	}
	return texts;
};
/*function getIds(){
	var idStr=null;
	if($("#"+idField).val){
		idStr=$("#"+idField).val();
	}else{
		idStr=$("#"+idField).html();
	}
	return idStr;
}*/
/*
 * 用打开窗口的方式回调的方法
 */
$.btAddressBook.winCallback = function(msg){
 	if(msg.hasOk){
  		msg.ids = $.btAddressBook.compIds(msg.data);
		msg.texts = $.btAddressBook.compTexts(msg.data);
		msg.values = $.btAddressBook.compValues(msg.data);
			if($("#"+$.sdAddressBook.settings.idFieldId).val){
				$("#"+$.sdAddressBook.settings.idFieldId).val(msg.ids);
			}else{
				$("#"+$.sdAddressBook.settings.idFieldId).html(msg.ids);
			}
			if($("#"+$.sdAddressBook.settings.textFieldId).val){
				$("#"+$.sdAddressBook.settings.textFieldId).val(msg.texts);
			}else{
				$("#"+$.sdAddressBook.settings.textFieldId).html(msg.texts);
			}
			if($.sdAddressBook.settings.callback){
				$.sdAddressBook.settings.callback(msg);
			}
	}
};

/*
 * 例子：
 * $(this).btAddressBook({
 * 	openType : "dialog",
 * 	type : 2,
 * 	selectType:20,
 * 	callback:function(msg){ }
 * })
 */
$.extend($.btAddressBook, {
	defaults:{
		title:"我的地址本",//定义打开地址本的标题
		openType : "windows", //打开方式 dialog:弹出框  windows:打开新页面（默认打开新页面）
		url :  baseURL+"/btaddressbook/btaddressbook/frame.do",
		type : 0,//显示的面板,0表示所有面板(默认),可多选，用分号分割，如"2;3", 其他数值则根据地址本的注册类有所区别，默认地址本注册类（BtAddressBookRegister）
		userPermission : false,//权限校验，默认关闭
		defaultChecked : 0,//默认展开的面板,根据type的值来填
		selectType : null,//确定数据范围，根据地址本的注册类有所区别，默认地址本注册类（BtAddressBookRegister）
		num: 0, //限制地址本选择的对象数（默认值0）,小于等于0代表不限制选定对象个数
		idFieldId : "",//回显所选数据的id的input或div的id	数据格式 id1$id2&id3
		textFieldId : "",//回显所选数据的名称的input或div的id	数据格式 name1、name2、name3
		dataFieldId : "",//回显数据使用input或div的id，回显的数据格式org!@!工程管理部!@!402881695a7fa028015a83d21c7e0020,org!@!马店长!@!402881695a7fa028015a83d4e2890022
		filterId : "",//过滤数据使用input或div的id，数据格式org!@!工程管理部!@!402881695a7fa028015a83d21c7e0020,org!@!马店长!@!402881695a7fa028015a83d4e2890022
		includeParent : 1,//点击节点时，是否包含父节点在待选框 1包含 0不包含 默认包含
		defaultParams : null,//默认查询参数，数据格式参考搜索组件的数据格式
		registerDicCode : null,//自定义注册bean的名称的数据字典值（如果需要自定义地址本注册类，模仿BtAddressBookRegister，并将改bean的名字保存在数据字典ADDRESS_BOOK_REGISTER中）
		treeLevels : null,//待选框默认显示的层级 -1：全部  0：当前节点（默认） "2;3"则显示2 3级
		nodeCode : null,//初始化节点编码，用于自定义展示的根节点,根据地址本的注册类实现的查询方法有所区别
		callback : function(msg){//地址本回调方法，点击地址本确定按钮时调用，msg就是已选框中的数据，格式为数组
			
		}
	},
	prototype: {
		init: function() {
			var idFieldId=this.settings.idFieldId;
			var textFieldId=this.settings.textFieldId;
			var dataFieldId=this.settings.dataFieldId;
			var callback=this.settings.callback;
			BootstrapDialog.show({
				title: this.settings.title,
				size: BootstrapDialog.SIZE_WIDER,
			    message: function(dialog) {
			        var $message = $('<div></div>');
			        var pageToLoad = dialog.getData('pageToLoad');
			        $message.load(pageToLoad);
			        return $message;
			    },
			    data: {
			        'pageToLoad': this.settings.url+(this.settings.url.indexOf("?")>0 ? "&" : "?")+$.param(params),
			    },
		        buttons: [{
		            label: '提交',
		            cssClass: 'btn-primary',
		            action: function(dialog){
		            	confirm(function(msg){
		            		if(msg.hasOk){
		            			dialog.close();
		            			msg.ids = $.btAddressBook.compIds(msg.data);
		            			msg.texts = $.btAddressBook.compTexts(msg.data);
		            			msg.values = $.btAddressBook.compValues(msg.data);
		            				if($("#"+idFieldId).val){
		            					$("#"+idFieldId).val(msg.ids);
		            				}else{
		            					$("#"+idFieldId).html(msg.ids);
		            				}
		            				if($("#"+textFieldId).val){
		            					$("#"+textFieldId).val(msg.texts);
		            				}else{
		            					$("#"+textFieldId).html(msg.texts);
		            				}
		            				//回显
		            				var str = $.btAddressBook.compValues(msg.data);
		            				if($("#"+dataFieldId).val){
		            					$("#"+dataFieldId).val(str);
		            				}else{
		            					$("#"+dataFieldId).html(str);
		            				}
		            			if(callback){
		            				callback(msg);
		            			}
		            		}
		            	});
		            }
		        }, {
		            label: '取消',
		            cssClass: 'btn-orange',
		            action: function(dialogItself){
		                dialogItself.close();
		            }
		        }]
			});
		},
		initWindows:function(){
			window.open(this.settings.url+(this.settings.url.indexOf("?")>0 ? "&" : "?")+$.param(params), "_blank");
		}
	}
});
(function($){
	$.extend($.fn, {
		btAddressBook : function(options){
			return new $.btAddressBook(options);
		}
	})
})(jQuery);

